﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FAS.App_Code;
using FAS.Objetos;
using FAS;
using System.IO;
using System.Net;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;

namespace FasGUI
{
    public partial class NuevoProblemaRTF : Form
    {
        Form1 filtro;
        public NuevoProblemaRTF(Form1 form,String rtf,String objetivo, int materia)
        {
            this.filtro = form;
            // TODO: Complete member initialization
            InitializeComponent();
            this.Show(); 
            this.objetivo = objetivo;
            this.materia = materia;
            fill(rtf);
            
        }

        private void fill(string rtf)
        {

            string sA = "A)";
            string sB = "B)";
            string sC = "C)";
            string sD = "D)";
            values(ref sA, ref sB, ref sC, ref sD, rtf);
            string opcionA = "";
            string opcionB = "";
            string opcionC = "";
            string opcionD = "";
            string[] vecA = rtf.Split(new string[] { sA }, StringSplitOptions.None);
            if (vecA.Length > 1)
            {
                fix(sA, ref vecA);
                opcionA = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecA[1];
                string[] vecB = opcionA.Split(new string[] { sB }, StringSplitOptions.None);
                fix(sB, ref vecB);
                opcionA = vecB[0];
                opcionB = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecB[1];
                string[] vecC = opcionB.Split(new string[] { sC }, StringSplitOptions.None);
                fix(sC, ref vecC);
                opcionB = vecC[0];
                if (vecC.Length != 1)
                {
                    opcionC = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecC[1];
                    string[] vecD = opcionC.Split(new string[] { sD }, StringSplitOptions.None);
                    fix(sC, ref vecD);
                    opcionC = vecD[0];
                    opcionD = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecD[1];
                }
                else 
                {
                    vecA = rtf.Split(new string[] { sA }, StringSplitOptions.None);
                    fix(sA, ref vecA);
                    opcionA = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecA[1];
                    vecB = opcionA.Split(new string[] { sC }, StringSplitOptions.None);
                    fix(sB, ref vecB);
                    opcionA = vecB[0];
                    opcionB = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecB[1];
                    vecC = opcionB.Split(new string[] { sB }, StringSplitOptions.None);
                    fix(sC, ref vecC);
                    opcionB = vecC[0];
                    opcionC = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecC[1];
                    string[] vecD = opcionC.Split(new string[] { sD }, StringSplitOptions.None);
                    fix(sC, ref vecD);
                    opcionC = vecD[0];
                    opcionD = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3082{\\fonttbl{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fnil\\fcharset0 Arial;}}" + vecD[1];
                    string temp = opcionB;
                    opcionB = opcionC;
                    opcionC = temp;
              
                }
            }
            txtProblema.Rtf = vecA[0];
            txtOpcionA.Rtf = opcionA;
            txtOpcionB.Rtf = opcionB;
            txtOpcionC.Rtf = opcionC;
            txtOpcionD.Rtf = opcionD;
           // System.Threading.Thread.Sleep(1500);
            
            btnGuardar_Click(null, null); 
        }

        private void fix(string s, ref string[] vec)
        {
            if (vec.Length > 2) 
            {
                String[] v = new String[2];
                v[0] = vec[0];
                String all = "";
                for (int x = 1; x < vec.Length; x++)
                    all += vec[x];
                v[1] = all;
                vec = v;
            }
        }

        private void values(ref string sA, ref string sB, ref string sC, ref string sD, string rtf)
        {
            string[] v1 = rtf.Split(new string[] { "A)" }, StringSplitOptions.None);
            if (v1.Length == 1)
            {
                string[] v2 = rtf.Split(new string[] { "a)" }, StringSplitOptions.None);
                if (v2.Length == 1)
                {
                    string[] v3 = rtf.Split(new string[] { "A." }, StringSplitOptions.None);
                    if (v3.Length == 1)
                    {
                        sA = "a.";
                        sB = "b.";
                        sC = "c.";
                        sD = "d.";
                    }
                    else
                    {
                        sA = "A.";
                        sB = "B.";
                        sC = "C.";
                        sD = "D.";
                    }
                }
                else
                {
                    sA = "a)";
                    sB = "b)";
                    sC = "c)";
                    sD = "d)";
                }
            }
            else 
            {
                sA = "A)";
                sB = "B)";
                sC = "C)";
                sD = "D)";
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            if (true)
            {
                Problema p = new Problema("", objetivo, null,txtProblema.Rtf,materia);
                p.IdProblema = p.insertar();
                p.Nombre = "http://" + ConexionMySQL.ip + "/FAS/images/" + p.IdProblema + ".png";
                p.update();
                //ImageManipulator.banderaMedia = true;
                //ImageManipulator.generarImagenes(txtProblema, p.IdProblema.ToString());
                string opcionA = p.IdProblema + "-A";
                string opcionB = p.IdProblema + "-B";
                string opcionC = p.IdProblema + "-C";
                string opcionD = p.IdProblema + "-D";
                string pathA = "http://" + ConexionMySQL.ip + "/FAS/images/" + opcionA + ".png";
                string pathB = "http://" + ConexionMySQL.ip + "/FAS/images/" + opcionB + ".png";
                string pathC = "http://" + ConexionMySQL.ip + "/FAS/images/" + opcionC + ".png";
                string pathD = "http://" + ConexionMySQL.ip + "/FAS/images/" + opcionD + ".png";
                new Opcion(pathA, isAnswer(radioButton1), p.IdProblema, txtOpcionA.Rtf).insertar();
                new Opcion(pathB, isAnswer(radioButton2), p.IdProblema, txtOpcionB.Rtf).insertar();
                new Opcion(pathC, isAnswer(radioButton3), p.IdProblema, txtOpcionC.Rtf).insertar();
                new Opcion(pathD, isAnswer(radioButton4), p.IdProblema, txtOpcionD.Rtf).insertar();
                this.Dispose();
                filtro.richTextBox1.Rtf = "";
                filtro.GetClipBoardText();

            }
        }

        private bool isAnswer(RadioButton radioButton)
        {
            return radioButton.Checked;
        }

        private void NuevoProblema_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            filtro.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            filtro.Show();
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }


        public int materia { get; set; }

        public String objetivo { get; set; }
    }
}